MWF.xDesktop.notice = function(type, where, content, target){
	var noticeTarget = target || layout.desktop.desktopNode;
	new mBox.Notice({
		type: type,
		position: where,
		move: false,
		target: noticeTarget,
		offset: {
			x: 10,
			y: where.y.toString().toLowerCase()=="bottom" ? 10 : 10
		},
		content: content
	});
};
MWF.xDesktop.confirm = function(type, e, title, text, width, height, ok, cancel, callback, mask, style){
    MWF.require("MWF.xDesktop.Dialog", function(){
        var size = layout.desktop.node.getSize();
        var x = 0;
        var y = 0;

        if (typeOf(e)=="element"){
            var position = e.getPosition(layout.desktop.node);
            x = position.x;
            y = position.y;
        }else{
            if (Browser.name=="firefox"){
                x = parseFloat(e.event.clientX);
                y = parseFloat(e.event.clientY);
            }else{
                x = parseFloat(e.event.x);
                y = parseFloat(e.event.y);
            }

            if (e.target){
                var position = e.target.getPosition(layout.desktop.node);
                x = position.x;
                y = position.y;
            }
            //    }
        }

        if (x+parseFloat(width)>size.x){
            x = x-parseFloat(width);
        }
        if (x<0) x = 0;
        if (y+parseFloat(height)>size.y){
            y = y-parseFloat(height);
        }
        if (y<0) y = 0;

        var ctext = "";
        var chtml = "";
        if (typeOf(text).toLowerCase()=="object"){
            ctext = text.text;
            chtml = text.html;
        }else{
            ctext = text;
        }
        var dlg = new MWF.xDesktop.Dialog({
            "title": title,
            "style": style || "flat",
            "top": y,
            "left": x-20,
            "fromTop":y,
            "fromLeft": x-20,
            "width": width,
            "height": height,
            "text": ctext,
            "html": chtml,
            "container": MWF.xDesktop.node,
            "maskNode": mask,
            "buttonList": [
                {
                    "text": MWF.LP.process.button.ok,
                    "action": ok
                },
                {
                    "text": MWF.LP.process.button.cancel,
                    "action": cancel
                }
            ]
        });

        switch (type.toLowerCase()){
            case "success":
                dlg.content.setStyle("background-image", "url()");
                break;
            case "error":
                dlg.content.setStyle("background-image", "url()");
                break;
            case "info":
                dlg.content.setStyle("background-image", "url()");
                break;
            case "warn":
                dlg.content.setStyle("background-image", "url()");
                break;
            default:
            //dlg.content.setStyle("background-image", "");
        }
        dlg.show();
        if (callback) callback(dlg);
    }.bind(this));
};
MWF.xDesktop.getImageSrc = function( id ){
    var addressObj = layout.desktop.serviceAddressList["x_file_assemble_control"];
    if (addressObj){
        var address = "http://"+addressObj.host+(addressObj.port==80 ? "" : ":"+addressObj.port)+addressObj.context;
    }else{
        var host = layout.config.center.host || window.location.hostname;
        var port = layout.config.center.port;
        var address = "http://"+host+(port=="80" ? "" : ":"+port)+"/x_program_center";
    }
    var url = "/servlet/file/download/"+id+"/stream";
    return address+url;
};
MWF.xDesktop.setImageSrc = function(){
    if( !event )return;
    var obj = event.srcElement ? event.srcElement : event.target;
    if( !obj )return;
    obj.onerror = null;
    var id = obj.get("data-id");
    if( id )obj.set("src" , MWF.xDesktop.getImageSrc(id) );
};
MWF.xDesktop.uploadImage = function( reference, referencetype, formData, file, success, failure ){
    this.action = new MWF.xDesktop.Actions.RestActions("/xDesktop/Actions/action.json", "x_file_assemble_control");
    this.action.invoke({
        "name": "uploadImage",
        "parameter": {"reference" : reference, "referencetype": referencetype},
        "data": formData,
        "file": file,
        "success": success,
        "failure": failure
    });
};
MWF.xDesktop.uploadImageByScale = function( reference, referencetype, scale, formData, file, success, failure ){
    this.action = new MWF.xDesktop.Actions.RestActions("/xDesktop/Actions/action.json", "x_file_assemble_control");
    this.action.invoke({
        "name": "uploadImageByScale",
        "parameter": {"reference" : reference, "referencetype": referencetype, "scale" : scale || 0},
        "data": formData,
        "file": file,
        "success": success,
        "failure": failure
    });
};
MWF.xDesktop.copyImage = function( reference, referencetype, attachmentId, scale, success, failure ){
    this.action = new MWF.xDesktop.Actions.RestActions("/xDesktop/Actions/action.json", "x_file_assemble_control");
    this.action.invoke({
        "name": "copyImage",
        "parameter": {"reference" : reference, "referencetype": referencetype, "attachmentId" : attachmentId, "scale":scale || 0 },
        "success": success,
        "failure": failure
    });
};
MWF.xDesktop.getServiceAddress = function(config, callback){
    if (typeOf(config.center)=="object"){
        MWF.xDesktop.getServiceAddressConfigObject(callback);
    }else if (typeOf(config.center)=="array"){
        var center = MWF.xDesktop.chooseCenter(config);
        if (center){
            MWF.xDesktop.getServiceAddressConfigObject(callback, center);
        }else{
            MWF.xDesktop.getServiceAddressConfigArray(config, callback);
        }
    }
};
MWF.xDesktop.chooseCenter = function(config){
    var host = window.location.host;
    var center = null;
    for (var i=0; i<config.center.length; i++){
        var ct = config.center[i];
        if (ct.webHost==host){
            center = ct;
            break;
        }
    }
    return center;
};
MWF.xDesktop.getServiceAddressConfigArray = function(config, callback) {
    var requests = [];
    config.center.each(function(center){
        requests.push(
            MWF.xDesktop.getServiceAddressConfigObject(function(serviceAddressList, center){
                requests.each(function(res){
                    if (res.isRunning()){res.cancel();}
                });
                if (callback) callback(serviceAddressList, center);
            }.bind(this), center)
        );
    }.bind(this));
};
MWF.xDesktop.getServiceAddressConfigObject = function(callback, center){
    var centerConfig = center;
    if (!centerConfig) centerConfig = layout.config.center;
    var host = centerConfig.host || window.location.hostname;
    var port = centerConfig.port;
    var uri = "";
    if (!port || port=="80"){
        uri = "http://"+host+"/x_program_center/jaxrs/distribute/assemble/source/{source}";
    }else{
        uri = "http://"+host+":"+port+"/x_program_center/jaxrs/distribute/assemble/source/{source}";
    }
    var currenthost = window.location.hostname;
    uri = uri.replace(/{source}/g, currenthost);
    //var uri = "http://"+layout.config.center+"/x_program_center/jaxrs/distribute/assemble";
    debugger;
    return MWF.restful("get", uri, null, function(json){
        //this.serviceAddressList = json.data;
        //this.centerServer = center;
        if (callback) callback(json.data, center);
    }.bind(this));
};